package com.dyh.quickdemo.IdLock;

import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.ReentrantLock;

/*
@Author=Joey Wheeler
@Date=2021/5/20 13:15
*/
@Slf4j
public class IdLockTest {

    @Test
    public void test2() throws InterruptedException {
        IdLock idLock = new IdLock();

        Thread t1 = new Thread(()->
        {
            log.info("res={}", idLock.tryLock(1));
            try {
                Thread.sleep(500L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            log.info("unlock");
            idLock.tryUnlock(1);
        }

        );
        Thread t2 = new Thread(()->log.info("res={}", idLock.tryLock(2)));
        Thread t3 = new Thread(()->log.info("res={}", idLock.tryLock(3)));
        Thread t4 = new Thread(()->log.info("res={}", idLock.tryLock(1)));
        Thread t5 = new Thread(()->log.info("res={}", idLock.tryUnlock(1)));
        Thread t6 = new Thread(()->{
            try {
                Thread.sleep(600L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            log.info("res={}", idLock.tryLock(1));
        });


        t1.start();

        Thread.sleep(100L);//保证t1先跑

        t2.start();
        t3.start();
        t4.start();//false
      //  t5.start();//IllegalMonitorStateException
        t6.start();


        Thread.sleep(1000L);
        /*这里打断点 debug各个线程*/



    }

    int sate;
    @Test
    public void test() throws InterruptedException {
       List<Thread> threads = new ArrayList<>();
        for (int i =0;i<3;i++){
            int finalI = i;
//            new Thread(()->{
//                log.info("set");
//                sate |=1 << finalI;
//            }).start();

            threads.add(
                    new Thread(()->{
                log.info("set");
                sate |=1 << finalI;
            }));
        }

        threads.forEach(Thread::start);

//        System.out.println(threads.size());
        Thread.sleep(100L);
        System.out.println(sate);
    }
}
